<?php
/**
 * 模型通用方法
 *
 * Author:李强 EdwinLee
 * Company:上海煦鸣网络科技有限公司
 * Time: 2018-01-10 21:25
 */

namespace App\Traits;

use App\Models\Area;
use App\Models\Member;
use App\Services\Service\Helper;

trait Common
{
    /**
     * 查询所有数据
     *
     * @param array $wheres
     * @param array $orderBys
     * @param int $limit
     * @return mixed
     */
    public static function queryAll($wheres = [], $orderBys = [], $limit = 10)
    {
        $model = self::where('id', '>', 0);
        if (!empty($wheres)) {
            foreach ($wheres as $method => $params) {
                foreach ($params as $param) {
                    if (is_array($param) && !Helper::isAssoc($param)) {
                        $model->{$method}(...$param);
                    } else {
                        $model->{$method}($param);
                    }
                }
            }
        }
        if (!empty($orderBys)) {
            foreach ($orderBys as $orderBy) {
                $model->orderBy($orderBy[0], $orderBy[1]);
            }
        }else{
            $model->orderBy('id','desc');
        }

        $counts = $model->count();
        if (is_array($limit)) {
            $model->offset($limit[0])->limit($limit[1]);
        } else {
            $model->limit($limit);
        }
        $datas = $model->get();
        return ['datas' => $datas, 'counts' => $counts];
    }

    public function param_options()
    {
        return [];
    }

    /**
     * 获取分类的参数配置
     *
     * @param string $type
     * @return array
     */
    public static function params($type = '')
    {
        $self = new static();
        $params = $self->param_options();
        if ($type != '') {
            return isset($params[$type]) ? $params[$type] : [];
        } else {
            return $params;
        }
    }

    /**
     * 解析JSon数据
     *
     * @param $field
     * @return array|mixed
     */
    public function parseJson($field)
    {
        if (isset($this->{$field})) {
            $data = json_decode($this->{$field}, true);
            return is_null($data) ? [] : $data;
        }
        return [];
    }

    /**
     * 显示参数内容
     *
     * @param $field
     * @param string $default
     * @return string
     */
    public function viewcontent($field, $default = '')
    {
        $value = isset($this->{$field}) ? $this->{$field} : '';
        $default = $default ? $default : $value;
        if (method_exists($this, 'param_options')) {
            $options = $this->param_options();
            if (isset($options[$field][$value])) {
                return $options[$field][$value];
            } else {
                return $default;
            }
        } else {
            return $default;
        }
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeNormal($query)
    {
        return $query->where('status', '!=', -1);
    }

    /**
     * 所属地区
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function area()
    {
        return $this->belongsTo(Area::class, 'area_id', 'area_id');
    }

    /**
     * 所属省份
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function province()
    {
        return $this->belongsTo(Area::class, 'province_id', 'area_id');
    }

    /**
     * 所属城市
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function city()
    {
        return $this->belongsTo(Area::class, 'city_id', 'area_id');
    }

    /**
     * 城市
     *
     * @return string
     */
    public function location()
    {
        return (is_null($this->province)?'':$this->province->area_name . " " ). $this->city->area_name . " " . $this->area->area_name;
    }

    /**
     * 用户信息
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

    /**
     * 根据shopid删除相关数据
     *
     * @param $shop_id
     */
    public static function delByShopId($shop_id)
    {
        return self::where('shop_id', $shop_id)->delete();
    }
}